import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Dialog',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        splashColor: Colors.transparent,
      ),
      home: HomePage(),
    );
  }
}

enum Option { A, B, C }
enum Action { OK, CANCEL }

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String _choice = "Nothing";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Dialog'),
      ),
      body: Container(
        padding: EdgeInsets.all(16),
        alignment: Alignment.center,
        child: Column(
          children: [
            Text('Your choice is $_choice'),
            ElevatedButton(onPressed: _openSimpleDialog, child: Text('选项对话框')),
            ElevatedButton(
                onPressed: _showAlertDialog, child: Text('AlertDialog'))
          ],
        ),
      ),
    );
  }

  // 选项对话框
  Future _openSimpleDialog() async {
    // 选择的结果为异步
    final option = await showDialog(
        context: context,
        builder: (ctx) {
          return SimpleDialog(
            title: Text('SimpleDialog'),
            children: [
              SimpleDialogOption(
                child: Text('Option A'),
                onPressed: () {
                  // 关闭对话框，并异步返回指定值
                  Navigator.of(context).pop(Option.A);
                },
              ),
              SimpleDialogOption(
                child: Text('Option B'),
                onPressed: () {
                  // 关闭对话框，并异步返回指定值
                  Navigator.of(context).pop(Option.B);
                },
              ),
              SimpleDialogOption(
                child: Text('Option C'),
                onPressed: () {
                  // 关闭对话框，并异步返回指定值
                  Navigator.of(context).pop(Option.C);
                },
              ),
            ],
          );
        });

    // 处理返回的结果
    switch (option) {
      case Option.A:
        _choice = "A";
        break;
      case Option.B:
        _choice = "B";
        break;
      case Option.C:
        _choice = "C";
        break;
    }
    setState(() {});
  }

  Future _showAlertDialog() async {
    final action = await showDialog(
        context: context,
        // 是否允许点击空白处关闭对话框，false不允许
        barrierDismissible: false,
        builder: (ctx) {
          return AlertDialog(
            title: Text('温馨提示'),
            content: Text('确定删除吗？'),
            actions: [
              TextButton(
                  onPressed: () => Navigator.of(context).pop(Action.OK),
                  child: Text('ok')),
              TextButton(
                  onPressed: () => Navigator.of(context).pop(Action.CANCEL),
                  child: Text('cancel')),
            ],
          );
        });
    switch (action) {
      case Action.OK:
        _choice = "删除";
        break;
      case Action.CANCEL:
        _choice = "取消";
        break;
    }

    setState(() {});
  }
}
